﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using agvcontroller.Common;
using agvcontroller.Common.DataBase;
using agvcontroller.Log;

namespace agvcontroller.Common
{
    public class CCross
    {
        public int ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public int Center
        {
            get;
            set;
        }

        public int East
        {
            get;
            set;
        }

        public int West
        {
            get;
            set;
        }

        public int South
        {
            get;
            set;
        }

        public int North
        {
            get;
            set;
        }



    }

    public class CCrossDic : Dictionary<int, CCross>
    {
        private static CCrossDic uniqueInstance;
        CAGVCDB AGVCDB;
        Logger Log;

        public static CCrossDic getInstance()
        {
            if (uniqueInstance == null)
            {
                uniqueInstance = new CCrossDic();
            }
            return uniqueInstance;
        }

        public CCrossDic()
        {
            AGVCDB = CAGVCDB.getInstance();

            Log = new Logger("CrossDic");

            GetDataFromDB();
        }

        public CCross GetCrossByCenterNodeID(int nodeid)
        {
            foreach (KeyValuePair<int, CCross> cross in this)
            {
                if(cross.Value.Center == nodeid)
                    return cross.Value;
            }

            return null;
        }

        int GetDataFromDB()
        {
            DataTable table = AGVCDB.AGVCDataSet.Tables["NodeList"];

            CCross cross;

            try
            {
                foreach (DataRow row in table.Rows)
                {
                    cross = new CCross();

                    cross.ID = Convert.ToInt32(row["ID"]);
                    cross.Name = Convert.ToString(row["NAME"]);
                    cross.East = Convert.ToInt32(row["EAST"]);
                    cross.West = Convert.ToInt32(row["WEST"]);
                    cross.South = Convert.ToInt32(row["SOUTH"]);
                    cross.North = Convert.ToInt32(row["NORTH"]);
                    cross.Center = Convert.ToInt32(row["CENTER"]);

                    this.Add(cross.ID, cross);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(string.Format("{0},{1}", ex.Message, ex.StackTrace));
                return CReturnCode.ERR_DATA_ACQUIRING_FAIL;

            }

            return CReturnCode.SUCCESS;
        }

        int SetDataToDB()
        {
            return CReturnCode.SUCCESS;
        }
    }
}
